//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using Microsoft.Research.ScientificWorkflow.WebClient.Common;
using Microsoft.Research.ScientificWorkflow.WorkflowApplicationCommonSL;

namespace Microsoft.Research.ScientificWorkflow.WebClient.Views.WorkflowApplication
{
    /// <summary>
    /// Represents Toolbar View in the trident work flow application
    /// </summary>
    public partial class ToolbarView : UserControl, IView
    {
        #region Private fields
        private TridentApplication application;
        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ToolbarView"/> class.
        /// </summary>
        public ToolbarView()
        {
            InitializeComponent();
            this.AttachCommands();
        }
        #endregion

        #region IView Members

        /// <summary>
        /// Fired for launching of the wait screen.
        /// </summary>
        public event EventHandler LaunchWaitScreen;

        /// <summary>
        /// Fired for removing of the wait screen.
        /// </summary>
        public event EventHandler RemoveWaitScreen;

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public FrameworkElement View
        {
            get
            {
                return this;
            }
        }

        /// <summary>
        /// Initializes this View.
        /// </summary>
        public void Initialize()
        {
            this.application = TridentApplication.Instance;
            this.btnTerminate.IsEnabled = false;
            this.tbxUserName.Text = this.application.UserDisplayName;

            this.hbtnSignIn.Click += new RoutedEventHandler(OnSignInClick);
            this.hbtnSignOut.Click += new RoutedEventHandler(OnSignOutClick);

            this.LaunchWaitScreen += delegate(object sender, EventArgs e) { };
            this.RemoveWaitScreen += delegate(object sender, EventArgs e) { };
            DetermineButtonAccessibility();
        }

        #endregion

        #region private methods

        /// <summary>
        /// 
        /// </summary>
        private void AttachCommands()
        {
            Commands.SetCommand(this.btnExecute, JobCommands.ExecuteCommand);
            Commands.SetCommand(this.btnTerminate, JobCommands.TerminateCommand);
        }

        /// <summary>
        /// Determines the button accessibility.
        /// </summary>
        private void DetermineButtonAccessibility()
        {
            if (this.application.IsAuthenticated)
            {
                this.hbtnSignIn.Visibility = Visibility.Collapsed;
                this.hbtnSignOut.Visibility = Visibility.Visible;
            }
            else
            {
                this.hbtnSignIn.Visibility = Visibility.Visible;
                this.hbtnSignOut.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSignInClick(object sender, RoutedEventArgs e)
        {
            Uri targetUri = new Uri(this.application.HostUri + Constants.UriSeparator + Constants.SignInPageName, UriKind.Absolute);
            HtmlPage.Window.Navigate(targetUri);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSignOutClick(object sender, RoutedEventArgs e)
        {
            this.application.LogOff();
        }

        #endregion
    }
}
